package cn.jdy.plugins.service;

import java.util.List;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.plugins.entity.RelatedVideo;

/***
 * @desc  相关实现类
 * @author lph
 *
 */
@Service
public class RelatedVideoService extends BaseDao<RelatedVideo>{
	/**
	 * 按条件查询
	 * @param TeachAdvice
	 * @return
	 */
	public List<RelatedVideo> query(RelatedVideo relatedVideo, String... date) {
		if(null == relatedVideo) return super.findAll();
		if(relatedVideo.getiDisplayLength() > 0){
		  return super.find(createFilter(relatedVideo, date),relatedVideo.getiDisplayStart(),relatedVideo.getiDisplayLength());
		}
		return super.find(createFilter(relatedVideo, date));
	}

	public Long count(RelatedVideo relatedVideo, String... date) {
		return super.count(createFilter(relatedVideo, date));
	}
	
	/**
	 * 拼装条件
	 * @param evaluationProject
	 * @param date
	 * @return
	 */
	private DetachedCriteria createFilter(RelatedVideo relatedVideo, String... date) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);	
	  
	
	  if(!StringUtil.isNullOrEmpty(relatedVideo.getVideoName())){
		  dc.add(Restrictions.like("videoName", relatedVideo.getVideoName().trim(), MatchMode.ANYWHERE));
	  }
	  if(!StringUtil.isNullOrEmpty(relatedVideo.getLiveId())){
		  dc.add(Restrictions.eq("liveId", relatedVideo.getLiveId()));
	  }
	  if(!StringUtil.isNullOrEmpty(relatedVideo.getMainid())){
		  dc.add(Restrictions.eq("mainid", relatedVideo.getMainid()));
	  }
	  if(!StringUtil.isNullOrEmpty(relatedVideo.getBackid())){
		  dc.add(Restrictions.eq("backid", relatedVideo.getBackid()));
	  }
	  if(!StringUtil.isNullOrEmpty(relatedVideo.getLiveName())){
		  dc.add(Restrictions.like("liveName", relatedVideo.getLiveName().trim(), MatchMode.ANYWHERE));
	  }
	  if (date.length > 0 && !StringUtil.isNullOrEmpty(date[0])&& !StringUtil.isNullOrEmpty(date[1])) {
			dc.add(Restrictions.between("createTime", date[0], date[1]));
		}
	  dc.addOrder(Order.desc("createTime"));
		return dc;
	}

	/***
	 * @author lph
	 * @desc 查询最大的时间。
	 * @return
	 */
	public String queryUptime() {
	    
		SQLQuery query = super.em().createSQLQuery("select max(createTime) from RelatedVideo ");

        return String.valueOf(query.uniqueResult());
	  }
	

	/***
	 * @author lph
	 * @desc 查询最大的时间。
	 * @return
	 */
	public String queryMaxTime(Long liveId) {
	    
		SQLQuery query = super.em().createSQLQuery("select max(createTime) from RelatedVideo where liveId="+liveId);

        return String.valueOf(query.uniqueResult());
	  }
	
	
	/***
	 * 
	 * 清空所有相关视频选中状态并初始化为0
	 */
	public void updateVideo() {
		try {
			String sql = "delete from relatedvideo";
			SQLQuery sqlQuery = em().createSQLQuery(sql);
			sqlQuery.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
		}	
	}
	
	/***
	 * 
	 * 清空所有相关直播选中状态并初始化为0
	 */
	public void updateLive() {
		try {
			String sql = "update live set currentStatus=0";
			SQLQuery sqlQuery = em().createSQLQuery(sql);
			sqlQuery.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
		}	
	}
}
